package leetcode.editor.cn;

import java.util.HashSet;
import java.util.Set;

//Java：两个数组的交集
public class IntersectionOfTwoArrays {
    public static void main(String[] args) {
        Solution solution = new IntersectionOfTwoArrays().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
            if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
                return new int[0];
            }
            Set<Integer> set1 = new HashSet<>();
            Set<Integer> resSet = new HashSet<>();
            for (int i : nums1) {
                set1.add(i);
            }
            for (int i : nums2) {
                if (set1.contains(i)) {
                    resSet.add(i);
                }
            }
            return resSet.stream().mapToInt(x -> x).toArray();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}